from datetime import datetime

from django.db import models

from organization.models import CourseOrg,Teacher
# Create your models here.


class Course(models.Model):
    name = models.CharField(max_length=50,verbose_name=u"课程表")
    desc = models.CharField(max_length=300,verbose_name=u"课程描述")
    detail = models.TextField(verbose_name=u"课程详情")  #不限长度
    teacher = models.ForeignKey(Teacher,verbose_name='讲师',null=True,blank=True,on_delete=models.CASCADE)
    degree = models.CharField(verbose_name=u"难度",choices=(("cj","初级"),("zj","中级"),("gj","高级")),max_length=2)
    learn_times = models.IntegerField(default=0,verbose_name=u"学习时长（分钟）")
    students = models.IntegerField(default=0,verbose_name=u"学习人数")
    fav_nums = models.IntegerField(default=0,verbose_name=u"收藏人数")
    image = models.ImageField(upload_to="courses/%Y/%m",verbose_name=u"封面")
    click_nums = models.IntegerField(default=0,verbose_name=u"点击数")
    add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间")
    course_org = models.ForeignKey(CourseOrg,verbose_name="课程机构",on_delete=models.CASCADE,null=True,blank=True)
    category = models.CharField(max_length=20,verbose_name=u"课程类别",default="后端开发")
    tag = models.CharField(default="",verbose_name=u"课程标签",max_length=10)
    youneed_know = models.CharField('课程须知', max_length=300, default='')
    teacher_tell = models.CharField('老师告诉你', max_length=300, default='')

    class Meta:
        verbose_name = u"课程"
        verbose_name_plural = verbose_name

    def get_zj_nums(self):
        #获取章节数
        return self.lesson_set.all().count()

    def get_learn_users(self):
        return self.usercourse_set.all()[:5]

    def get_course_lesson(self):
        #获取所有的章节
        return self.lesson_set.all()

    def __str__(self):
        return self.name


#课程
class Lesson(models.Model):
    name = models.CharField(max_length=100,verbose_name=u"章节名")
    course = models.ForeignKey(Course, verbose_name=u"课程", on_delete=models.CASCADE)
    add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间")

    class Meta:
        verbose_name = u"章节"
        verbose_name_plural = verbose_name

    def get_lesson_vedio(self):
        #获取章节所有视频
        return self.video_set.all()

    def __str__(self):
        return '《{0}》课程的章节 >> {1}'.format(self.course, self.name)

#课程视频
class Video(models.Model):
    lesson = models.ForeignKey(Lesson,verbose_name=u"章节",on_delete=models.CASCADE)
    name = models.CharField(max_length=100, verbose_name=u"视频名")
    url = models.CharField(max_length=200,default="",verbose_name=u"访问地址")
    learn_times = models.IntegerField("学习时长(分钟数)", default=0)
    add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")

    class Meta:
        verbose_name = u"视频"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

#课程资源
class CourseResource(models.Model):
    course = models.ForeignKey(Course, verbose_name=u"课程",on_delete=models.CASCADE)
    name = models.CharField(max_length=100, verbose_name=u"资源名")
    downlode = models.FileField(upload_to="course/resource/%Y/%m",verbose_name=u"资源文件",max_length=100)
    add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")

    class Meta:
        verbose_name = u"课程资源"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name